package com.jieao.backend.controller.site;

import com.jieao.biz.domain.SiteNotice;
import com.jieao.common.annotation.Log;
import com.jieao.common.core.controller.BaseController;
import com.jieao.common.core.domain.AjaxResult;
import com.jieao.common.core.page.TableDataInfo;
import com.jieao.common.enums.BusinessType;
import com.jieao.framework.util.ShiroUtils;
import com.jieao.site.service.ISiteNoticeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 前端通知消息管理
 * @Author weifanl
 * @Date2020/6/26 15:22
 **/
@Controller
@RequestMapping("/site/notice")
public class NoticeController extends BaseController {

    private String prefix = "backend/site/notice";

    @Autowired
    private ISiteNoticeService siteNoticeService;

    @RequiresPermissions("site:notice:view")
    @GetMapping()
    public String Notice() {
        return prefix + "/notice";
    }

    /**
     * 查询通知列表
     */
    @RequiresPermissions("site:notice:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SiteNotice notice) {
        startPage();
        List<SiteNotice> list = siteNoticeService.selectSiteNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 新增通知页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存通知
     */
    @RequiresPermissions("site:notice:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SiteNotice notice) {
        notice.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(siteNoticeService.insertSiteNotice(notice));
    }

    /**
     * 修改通知页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("notice", siteNoticeService.selectNoticeById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存通知
     */
    @Log(title = "修改保存通知", businessType = BusinessType.UPDATE)
    @RequiresPermissions("site:notice:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SiteNotice notice) {
        return toAjax(siteNoticeService.updateNotice(notice));
    }

    @Log(title = "删除通知", businessType = BusinessType.DELETE)
    @RequiresPermissions("site:notice:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(siteNoticeService.deleteNoticeByIds(ids));
    }
}
